package cn.itcast.bos.web.action.base;

import java.util.Collection;

import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.CustomEditorConfigurer;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.base.FixedAreaService;
import cn.itcast.bos.web.action.common.BaseAction;
import cn.itcast.crm.domain.Customer;

@Namespace("/")
@ParentPackage("json-default")
@Controller
@Scope("prototype")
public class FixedAreaAction extends BaseAction<FixedArea> {

	@Autowired
	private FixedAreaService fixedAreaService;
	
	private Integer courierId;
	private Integer takeTimeId;
	
	
	public void setCourierId(Integer courierId) {
		this.courierId = courierId;
	}

	public void setTakeTimeId(Integer takeTimeId) {
		this.takeTimeId = takeTimeId;
	}

	//关联快递员
	@Action(value = "fixedArea_associationCourierToFixedArea", results = { @Result(name = "success", type = "redirect",location="./pages/base/fixed_area.html") })
	public String associationCourier(){
		fixedAreaService.associationCourierToFixedArea(model,courierId,takeTimeId);
		return SUCCESS;
	}

	private String[] customerIds;
	
	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}

	//关联客户
	@Action(value = "fixedArea_associationCustomersToFixedArea", results = { @Result(name = "success", type = "redirect",location="./pages/base/fixed_area.html") })
	public String associationCustomersToFixedArea(){
		String customerIdStr = StringUtils.join(customerIds, ",");
		WebClient.create("http://localhost:9002/crm_management/services/customerService/associationcustomerstofixedarea?customerIdStr="
		+customerIdStr+"&fixedAreaId="+model.getId()).put(null);
		
		return SUCCESS;
	}
	
	// 查询未绑定定区的客户
	@Action(value = "fixedArea_findNoAssociationCustomers", results = { @Result(name = "success", type = "json") })
	public String findNoAssociationCustomers() {
		Collection<? extends Customer> customers = WebClient
				.create("http://localhost:9002/crm_management/services/customerService/noassociationcustomers")
				.accept(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(customers);
		return SUCCESS;
	}

	// 查询绑定当前定区的客户
	@Action(value = "fixedArea_hasAssociationCustomers", results = { @Result(name = "success", type = "json") })
	public String findHasAssociationCustomers() {
		System.out.println(model.getId());
		Collection<? extends Customer> customers = WebClient
				.create("http://localhost:9002/crm_management/services/customerService/associationfixedareacustomers/"
						+ model.getId())
				.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(customers);
		return SUCCESS;
	}

	@Action(value = "fixedArea_save", results = {
			@Result(name = "success", location = "./pages/base/fixed_area.html", type = "redirect") })
	public String save() {
		fixedAreaService.save(model);
		return SUCCESS;
	}

	// 分页条件查询定区
	@Action(value = "fixedArea_search", results = { @Result(name = "success", type = "json") })
	public String search() {
		Pageable pageable = new PageRequest(page - 1, rows);

		Page pageData = fixedAreaService.search(model, pageable);

		pushPageDataToValueStack(pageData);

		return SUCCESS;
	}
}
